<?php include_once dirname(__FILE__).'/header.php' ?>
<?php

$date_id = '';
$where = '';
$start_date = '';
$end_date = '';
if( isset($_GET['search_date']) ){
	$date_id = $_GET['search_date'].'-01';
	$start_date = $date_id;
	$end_date = $_GET['search_date'].'-'.date('t',strtotime($date_id));
	$where .=' AND date_id BETWEEN '.strtotime($start_date).' and '.strtotime($end_date).' ';
}






$data = $dbObj->fetch_array( 'books' , $where );
$count = count($data);
//支出
$expend_cash = 0;

//收入
$revenue_cash = 0;

if( count($data) > 0 ){
	foreach( $data as $v ){
		switch( $v['blance'] ){
			case 0:
				$revenue_cash += $v['money'];
			break;
			case 1:
				$expend_cash += $v['money'];
			break;
		}
	}
}


?>
<script type="text/javascript">
$(document).ready(function(){
	$('#tables').dataTable({
		//"lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
		ordering:  false,
		searching: false,
		stateSave: false,
		"lengthChange": false,
		language : {
			info :  '현재페이지 _PAGE_ , 전부 _TOTAL_ 게시물',
			emptyTable : '해당 매치 된 내용이 없습니다.',
			infoEmpty : '현재페이지 0 , 전부 0 페이지',
			
			"paginate": {
				"first":      "처음",
				"last":       "맨끝",
				"next":       "다음",
				"previous":   "이전"
			},
		}
		
	});
	$('#date_id_list').Zebra_DatePicker({
		//direction: 1 ,
		show_icon : false,
		months:["1월","2월","3월","4월","5월","6월","7월","8월","9월","10월","11월","12월"],
		days : ['일요일', '월요일', '화요일', '수요일', '목요일', '금요일', '토요일'],
		format: 'Y-m'
	});
});
</script>

<h2>장부관리</h2>

<div>
<form class="uk-form">
	<input type="text" name="search_date" id="date_id_list" value="<?php echo isset($_GET['search_date']) ? $_GET['search_date'] : '' ?>" readonly class="uk-form-small">
	<input type="submit" class="uk-button uk-button-primary uk-button-small"  value="검색">
	<input type="button" class="uk-button uk-button-success uk-button-small"  value="레셋" onclick="location.href='list.php'">
</form>
</div>
<table id="tables" cellpadding="0" cellspacing="0" border="0">
	<thead>
		<tr>
			<th>번호</th>
			<th>수입/지출</th>
			<th><input type="checkbox" name="" id="" value="" onclick="allSelect(this)"></th>
			<th>일자</th>
			<th>작성자</th>
			<th>금액</th>
			<th>기록일</th>
			<th>수정일</th>
			<th>관리</th>
		</tr>
	</thead>
	
	<tbody>
		<?php if( count($data) > 0 ){ ?>
		<?php foreach( $data as $item ){ ?>
		<tr>
			<td><?php echo $count-- ?></td>
			<td><?php echo $item['blance'] == 0 ? '수입' : '지출' ?></td>
			<td><input type="checkbox" name="id" id="" value="<?php echo $item['id'] ?>"></td>
			<td><?php echo date("Y-m-d",$item['date_id']) ?></td>
			<td><?php echo $item['user'] ?></td>
			<td><?php echo $item['blance'] == 0 ? '+' : '-' ?> <?php echo number_format($item['money']); ?> 원(元)</td>
			<td><?php echo date("Y-m-d H:i:s",$item['create_time'] )?></td>
			<td><?php echo date("Y-m-d H:i:s",$item['update_time'] )?></td>
			<td><a href="javascript:void(0);" onclick="EditBook('<?php echo $item['id'] ?>')">관리</a></td>
		</tr>
		<?php } //foreach ?>
		<?php } ?>
	</tbody>
	
	<tfoot>
	
	</tfoot>
</table>


<div class="btn-area">
	<input type="button" class="uk-button uk-button-primary uk-button-small" value="장부쓰기" onclick="CreateBook()">
	<input type="button" class="uk-button uk-button-small" value="선택삭제" onclick="del()">
</div>


<div class="sum-area">
	<table cellpadding="0" cellspacing="0" border="0" >
		<tbody>
			<tr>
				<th>수입(收入)</th>
				<td><span style="color:blue">+ <?php echo number_format($revenue_cash) ?></span> 원(元)</td>
			</tr>
			<tr>
				<th>지출(支出)</span></th>
				<td><span style="color:red">- <?php echo number_format($expend_cash) ?></span> 원(元)</td>
			</tr>
			<tr>
				<th>리윤(利润)</th>
				<td><?php echo number_format($revenue_cash - $expend_cash) ?> 원(元)</td>
			</tr>
		</tbody>
	</table>
</div>


<?php include_once dirname(__FILE__).'/footer.php' ?>